package com.leyou.auth.service;

import com.leyou.auth.config.JwtProperties;
import com.leyou.auth.feign.UserClient;
import com.leyou.common.pojo.UserInfo;
import com.leyou.common.util.JwtUtils;
import com.leyou.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * @author shkstart
 * @create 2020-05-10 16:03
 */

@Service
@EnableConfigurationProperties(JwtProperties.class)  //开启配置
public class AuthService {


    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties jwtProperties;


    public String valid(String username, String password) {

        User user1 = userClient.queryUser(username,password);
        if (user1 == null){
            return  null;
        }else {
            //生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user1.getId());
            userInfo.setUsername(user1.getUsername());
            try {
                String token = JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());
                return token;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}
